package com.hf.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.hf.dao.UserInfoDao;
import com.hf.entity.User;
import com.hf.utils.DateChangeString;

@WebServlet("/userinfo.do")
@MultipartConfig(
		location="/", fileSizeThreshold=1024*1024, 
		maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
public class UserInfoServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String type = req.getParameter("type");
		HttpSession session  = req.getSession();
		UserInfoDao userinfoDao = new UserInfoDao();
		int user_id =  (int) session.getAttribute("user_id");
		if("queryid".equals(type)){
			User user = new User();
			user = userinfoDao.queryid(user_id);
			getBirthday(user.getUser_birthday(),session);
			resp.sendRedirect("personal_data/basic_Information.jsp");
		}else if("save".equals(type)){
			User user = new User();
			user = getUserDate(req);
			userinfoDao.modify(user);
			user = userinfoDao.queryid(user_id);
			getBirthday(user.getUser_birthday(),session);
			session.setAttribute("user", user);
			resp.sendRedirect("personal_data/ji_bxx.jsp");
		}
	}
	
	private void getBirthday(Date User_birthday,HttpSession session){
		String birthday = DateChangeString.getDateToString(User_birthday);
		String[] strs = birthday.split("-");
		String year = strs[0];
		String month = strs[1];
		String day = strs[2];
		session.setAttribute("year", year);
		session.setAttribute("month", month);
		session.setAttribute("day", day);
	}
	
	private User getUserDate(HttpServletRequest req){
		HttpSession session = req.getSession();
		String user_name = req.getParameter("user_name");
		String user_sex = req.getParameter("user_sex");
		String year = req.getParameter("year");
		String month = req.getParameter("month");
		String day = req.getParameter("day");
		String province = req.getParameter("user_province");
		String city = req.getParameter("user_city");
		String area = req.getParameter("user_area");
		String user_birthday = year+"-"+month+"-"+day;
		String user_address = province+city+area;
		Date birthday = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		String fileName = null;
		try {
			Collection<Part> list = req.getParts();
			Part part = req.getPart("user_pic"); 
	        // 获取上传的文件名称
	        String filename = part.getSubmittedFileName();
	        // 创建要保存的文件对象
	        fileName = createName(filename);
	        File file = new File(createDir(getServletContext()), fileName);
	        // 保存文件
	        part.write(file.getAbsolutePath());
	        birthday = sdf.parse(user_birthday);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String user_pic = fileName;
		int user_id = (int) session.getAttribute("user_id");
		User user = new User(user_id,user_name,user_sex,birthday,user_address,user_pic);
		return user;
	}
	
	private File createDir(ServletContext context) {
        String filepath = context.getRealPath("/UserInfo/images");
        File file = new File(filepath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    // 创建文件名--区分同名文件,在文件名前加上当前的时间
    private String createName(String name) {
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime()) + "_" + name;
    }
	
}
